/*
============================================================================
Name        : BlockSetNetWork.cpp
Author      : 
Version     :
Copyright   : 
Description : CBlockSetNetWork implementation
============================================================================
*/

#include "BlockSetNetWork.h"
#include "BlockSetFont.h"
#include "BlockSetAP.h"
#include "BlockSetFirstPage.h"
#include "eStockappui.h"
#include "Global.h"
#include <eStock_L2hy.rsg>

CBlockSetNetWork::CBlockSetNetWork(CBaseBlock* bsBack):
CBlock(bsBack)
{
}

CBlockSetNetWork::~CBlockSetNetWork()
{
	for(TInt i=0; i<3; i++)
		delete m_Ctrls[i];
	//m_ApList.Close();
}

CBlockSetNetWork* CBlockSetNetWork::NewLC(CBaseBlock* bsBack)
{
	CBlockSetNetWork* self = new (ELeave)CBlockSetNetWork(bsBack);
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CBlockSetNetWork* CBlockSetNetWork::NewL(CBaseBlock* bsBack)
{
	CBlockSetNetWork* self=CBlockSetNetWork::NewLC(bsBack);
	CleanupStack::Pop(); // self;
	return self;
}

void CBlockSetNetWork::ConstructL()
{
	CBlock::ConstructL();

	CGlobal::GetDesFromID(m_strOK,LOC_BUF_MENU_SAVE);
	CGlobal::GetDesFromID(m_strCancel,LOC_BUF_MENU_CANCEL2);
	CGlobal::GetDesFromID(m_strTitle,LOC_BUF_MENU_SETTING);

	TBuf<16> temp;
	CGlobal::GetDesFromID(temp,LOC_BUF_STR_REFRESHFREQUENCY);
	m_inputfresh = CSingleLineTextInput::NewL(temp, 3);
	temp.Zero();
	temp.AppendNum(m_pGlobal->g_nRefreshInterval);
	m_inputfresh->SetText(temp);
	m_inputfresh->SetRange(3,100);
	m_Ctrls[0] = m_inputfresh;

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_TIMEOUT2);
	m_inputresend =  CSingleLineTextInput::NewL(temp, 3);
	temp.Zero();
	temp.AppendNum(m_pGlobal->g_nResendInterval);
	m_inputresend->SetText(temp);
	m_inputresend->SetRange(15,200);
	m_Ctrls[1] = m_inputresend;

	CGlobal::GetDesFromID(temp,LOC_BUF_STR_RESENDTIMES);
	m_inputresendtimes =  CSingleLineTextInput::NewL(temp, 2);
	temp.Zero();
	temp.AppendNum(m_pGlobal->g_nResendTimes);
	m_inputresendtimes->SetText(temp);
	m_inputresendtimes->SetRange(0,10);
	m_Ctrls[2] = m_inputresendtimes;

	if (CGlobal::Static()->m_osMajor == 3)
		m_Ctrls[0]->SetFocus(ETrue);
}

void CBlockSetNetWork::AddTabs()
{
	TBuf<8> label;
	CGlobal::GetDesFromID(label,LOC_BUF_STR_APSET);
	iTabbar->AppendTab(label, KTabSettingAp);
	CGlobal::GetDesFromID(label,LOC_BUF_MENU_TYPEFACE);
	iTabbar->AppendTab(label, KTabSettingFont);
	CGlobal::GetDesFromID(label,LOC_BUF_STR_FRESHSET);
	iTabbar->AppendTab(label, KTabSettingFresh);
    /*CGlobal::GetDesFromID(label,LOC_BUF_STR_FIRSTPAGESET);
    iTabbar->AppendTab(label, KTabSettingFirstPage);*/
}

void CBlockSetNetWork::HandleTabbarCommand(TInt aCommand)
{
	CBlock* block = NULL;
	switch (aCommand)
	{
	case KTabSettingFont:
		block = CBlockSetFont::NewL(m_pBlockBack);
		break;
	case KTabSettingAp:
		block = CBlockSetAP::NewL(m_pBlockBack);
		break;
	case KTabSettingFresh:
		block = CBlockSetNetWork::NewL(m_pBlockBack);
		break;
    case KTabSettingFirstPage:
        block = CBlockSetFirstPage::NewL(m_pBlockBack);
        break;
	default:
		break;
	}
	if (block)
		GetContainer()->AddBlock(block);
}

void CBlockSetNetWork::BeforeDraw(CFbsBitGc& g) const
{
	iTitlebar->SetTitleText(m_strTitle);
	CBlock::BeforeDraw(g);
}

void CBlockSetNetWork::SizeChanged()
{
	CBlock::SizeChanged();
	iToolbar->SetSoftKey(CToolbar::EOK, CToolbar::EBack);
	iTabbar->SetSelectedId(KTabSettingFresh);
	for (int i = 0; i<3; i++)
	{
		TInt y = m_rect.iTl.iY + 5 + i*(m_nLineHeight+4);
		m_Ctrls[i]->SetRect(0, y, m_nScreenWidth, y+m_nLineHeight);
	}
}

void CBlockSetNetWork::OnDraw(CFbsBitGc& g)const
{
	for(TInt i=0; i<3; i++)
		m_Ctrls[i]->Draw(g);
}

void CBlockSetNetWork::OnInput(const TDesC8& strInput)
{
	if (m_kb->m_bInputGoodCode)
	{
		CBlock::OnInput(strInput);
		return;
	}
	
	TBuf<32> buf;
	buf.Copy(strInput);
	static_cast<CSingleLineTextInput*>(m_Ctrls[m_nCurrent])->SetText(buf);
	DrawNow();
}

void CBlockSetNetWork::OnKeyEvent(const TInt& keyCode)
{
	if (keyCode==m_pGlobal->g_nKeyOK || keyCode==EStdKeyNkpAsterisk || keyCode == '*')
	{
		m_pGlobal->g_nRefreshInterval =m_inputfresh->GetIntValue();
		m_pGlobal->g_nResendInterval = m_inputresend->GetIntValue();
		m_pGlobal->g_nResendTimes = m_inputresendtimes->GetIntValue();
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}
	if (keyCode==m_pGlobal->g_nKeyCancel)
	{
		if(eStockUI->GetCurView() && eStockUI->GetCurView()->iContainer)
			eStockUI->GetCurView()->iContainer->DelBlock();
		return;
	}

	if (keyCode==m_pGlobal->g_nKeyUp || keyCode==m_pGlobal->g_nKeyDown)
	{
		TInt nOldCurrent = m_nCurrent;
		if (keyCode==m_pGlobal->g_nKeyUp)
		{
			m_nCurrent--;
			if (m_nCurrent<0)
			{
				m_nCurrent = 0;
				//iTabbar->SetFocus(ETrue);
				//DrawNow();
			}
		}
		else
		{
			m_nCurrent++;
			if (m_nCurrent > 2)
			{
				m_nCurrent = 2;
				//iToolbar->SetFocus(ETrue);
				//DrawNow();
			}
		}
		if (nOldCurrent!=m_nCurrent)
		{
			m_Ctrls[nOldCurrent]->SetFocus(EFalse);
			m_Ctrls[m_nCurrent]->SetFocus(ETrue);
			DrawNow();
		}
		return;
	}

	if(m_Ctrls[m_nCurrent]->OnKeyDown(keyCode))
		DrawNow();
}

void CBlockSetNetWork::OnMouseUp(const TPoint& aPt)
{
	TInt nPos = -1;
	for(TInt i=0; i<3; i++)
	{
		if(m_Ctrls[i]->m_rect.Contains(aPt))
		{
			nPos = i;
			break;
		}
	}
	if (nPos>=0)
	{
		m_Ctrls[m_nCurrent]->SetFocus(EFalse);
		m_nCurrent = nPos;
		m_Ctrls[m_nCurrent]->SetFocus(ETrue);
		m_kb->SetInputType(CKeyBoard::NUMERIC);
		DrawNow();
	}
}         
